package com.leoman.spinningmachine.tab3;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018-05-08.
 */
public class UpNickNameActivity extends BaseActivity{

    @BindView(R.id.tv_nick_name)
    EditText tv_nick_name;

    @BindView(R.id.btn_save)
    Button btn_save;
    private String newNickName;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_up_nick_name;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initTitle("昵称");
        tv_nick_name.setText(SharedPreferencesUtils.getInstance().getString(Constant.NICKNAME));
        btn_save.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_save)
    public void onClicked(View view){
        newNickName = tv_nick_name.getText().toString().trim();
        if(TextUtils.isEmpty(newNickName)){
            DialogUtils.getInstance().showResultDialog(this,"请输入昵称");
            return ;
        }
        if(!TextUtils.isEmpty(newNickName)&&newNickName.equals(SharedPreferencesUtils.getInstance().getString(Constant.NICKNAME))){
            DialogUtils.getInstance().showResultDialog(this,"新昵称与旧昵称一致");
            return ;
        }
        if(!TextUtils.isEmpty(newNickName)&&newNickName.length()<2){
            DialogUtils.getInstance().showResultDialog(this,"昵称需输入2~14个字");
            return ;
        }
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().upNickName(SharedPreferencesUtils.getInstance().getString("token"),newNickName,this,1);
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        DialogUtils.getInstance().cancel();
        if(1 == tag){
            SharedPreferencesUtils.getInstance().putString(Constant.NICKNAME,newNickName);
            ToastUtil.showToast(this,apiResponse.message,Toast.LENGTH_SHORT);
            finish();
        }
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        super.OnErrorData(code,tag);
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        super.OnFailureData(error,tag);
    }
}
